package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.bas.entity.BasGoods;
import com.kingint.mbg.bas.entity.BasWarrantyItems;
import com.kingint.mbg.bas.mapper.BasWarrantyItemsMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasWarrantyItemsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.tree.BasGoosTree;
import com.kingint.web.util.tree.TreeBo;
import com.kingint.web.util.tree.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 保修物品相关表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-21
 */
@Service
@Slf4j
public class BasWarrantyItemsServiceImpl extends ServiceImpl<BasWarrantyItemsMapper, BasWarrantyItems> implements BasWarrantyItemsService {

    @Resource
    private UmsAdminService adminService;
    @Resource
    private BasWarrantyItemsMapper warrantyItemsMapper;

    @Override
    public int insertWarrantyItemsInfo(BasWarrantyItems basWarrantyItems) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("该账号没有关联酒店id");
            return 0;
        }
        basWarrantyItems.setCreateTime(new Date());
        basWarrantyItems.setHotelId(hotelId);
        int insert = warrantyItemsMapper.insert(basWarrantyItems);
        return insert;
    }

    @Override
    public int updateWarrantyItems(BasWarrantyItems basWarrantyItems) {
        int i = warrantyItemsMapper.updateById(basWarrantyItems);
        return i;
    }

    @Override
    public int deleteWarrantyItems(Integer id) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        QueryWrapper<BasWarrantyItems> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",id).eq("hotel_id", hotelId);
        int count = this.count(queryWrapper);
        if(count>0){
            return -1;
        }
        BasWarrantyItems items = new BasWarrantyItems();
        items.setId(id);
        items.setStatus(1);//删除
        int i = warrantyItemsMapper.updateById(items);
        return i;
    }

    @Override
    public Object getWarrantyItemsList(BasWarrantyItems basWarrantyItems, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("该账号没有关联酒店id");
            return null;
        }
        //进行处理
//        IPage<BasWarrantyItems> page = new Page<>(pageNum, pageSize);
//        QueryWrapper<BasWarrantyItems> wrapper = new QueryWrapper<>();
//        wrapper.eq("hotel_id", hotelId)
//                .eq("status", 0)
//                .eq("pid", 0);
//        IPage<BasWarrantyItems> page1 = warrantyItemsMapper.selectPage(page, wrapper);//父类
//        List<BasWarrantyItems> records = page1.getRecords();
//        QueryWrapper<BasWarrantyItems> wrapper1 = new QueryWrapper<>();
//        wrapper.eq("hotel_id", hotelId)
//                .eq("status", 0);
//        List<BasWarrantyItems> warrantyItems = warrantyItemsMapper.selectList(wrapper1);
//        cancelWarrantyItems(records, warrantyItems);
        QueryWrapper<BasWarrantyItems> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("status", 0);
        List<BasWarrantyItems> list = this.list(wrapper);
        TreeUtil<BasWarrantyItems> basGoodsTreeUtil = new TreeUtil<BasWarrantyItems>("getId","getPid");

        List<TreeBo<BasWarrantyItems>> createTreeAsTreeEntity=null;
        List<BasWarrantyItems> list2=new ArrayList<>();
        try {

            createTreeAsTreeEntity = basGoodsTreeUtil.createTreeAsTreeEntity(new HashSet<BasWarrantyItems>(list));
            if(createTreeAsTreeEntity.size()>0)
                tree(list2,createTreeAsTreeEntity);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return K.ok().data(list2);
    }

    private void  tree(List<BasWarrantyItems> list,List<TreeBo<BasWarrantyItems>>  TreeBo){
        for (TreeBo<BasWarrantyItems> itemsTreeBo : TreeBo) {
            BasWarrantyItems title = itemsTreeBo.getTitle();
            List<TreeBo<BasWarrantyItems>> children = itemsTreeBo.getChildren();
            if(children!=null){
                tree(title.getChildren(),children);
            }
            list.add(title);
        }

    }

    @Override
    public List<BasWarrantyItems> getAllWarrantyInfo() {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        QueryWrapper<BasWarrantyItems> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("status", 0)
                .eq("pid", 0);
        List<BasWarrantyItems> records = warrantyItemsMapper.selectList(wrapper);
        QueryWrapper<BasWarrantyItems> wrapper1 = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("status", 0);
        List<BasWarrantyItems> warrantyItems = warrantyItemsMapper.selectList(wrapper1);
        cancelWarrantyItems(records, warrantyItems);
        return records;
    }

    public void cancelWarrantyItems(List<BasWarrantyItems> aClass, List<BasWarrantyItems> allList) {
        if (!CollectionUtils.isEmpty(aClass)) {
            //为一级菜单设置子菜单
            for (BasWarrantyItems info : aClass) {
                getChildList(info, allList);
            }
        }
    }

    public void getChildList(BasWarrantyItems info, List<BasWarrantyItems> aClass) {
        List<BasWarrantyItems> serviceClass = info.getWarrantyItems();
        if (CollectionUtils.isEmpty(serviceClass)) {
            serviceClass = new ArrayList<>();
        }
        for (BasWarrantyItems ser : aClass) {
            if (ser.getPid() == info.getId()) {
                serviceClass.add(ser);
            }
        }
        //递归查询
        if (serviceClass.size() != 0) {
            for (BasWarrantyItems child : serviceClass) {
                getChildList(child, aClass);
            }
        }
        info.setWarrantyItems(serviceClass);
    }
}
